package tema4;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name="FormatoFormularioServlet", urlPatterns={"/tema4/formatoFormulario"})
public class FormatoFormularioServlet extends HttpServlet {
    
    protected void procesaPeticion(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();        
        try {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet FormatoFormulario</title>");
            out.println("<link rel=\"stylesheet\" title=\"standar\" type=\"text/css\" href=\"/EjemploBasicoJavaEE/standar.css\">");
            out.println("</head>");
            out.println("<body>");
            
            String texto = request.getParameter("texto");
            String fuente = request.getParameter("fuente");
            String color = request.getParameter("color");
                   
            if(texto.isEmpty())
                out.println("<h2>Has de insertar algun texto!!</h2>");
            
            out.println("<h1>Su texto formateado:</h1>");
            out.print("<font color=\""+color+"\" type=\""+fuente+"\">");
            if(fuente.equals("negrita"))
                out.print("<b>"+texto+"</b>");
            if(fuente.equals("cursiva"))
                out.print("<i>"+texto+"</i>");
            out.println("</font>");
        } finally {
            out.println("</body>");
            out.println("</html>");
            out.close();
        }
    }
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        procesaPeticion(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        procesaPeticion(request, response);
    }

}
